package com.example.generator.service.impl;


import com.example.generator.entity.TableInfo;
import com.example.generator.entity.config.DataSourceConfig;
import com.example.generator.entity.vo.CodeParament;
import com.example.generator.enums.GeneratorTypeEnum;
import com.example.generator.service.ICodeGeneratorService;
import com.example.generator.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author：QPX
 * @date：2024-01-29-13:46
 */
@Service
@Slf4j
public class CodeGeneratorServiceImpl implements ICodeGeneratorService {

    @Override
    public TableInfo generatorInit(CodeParament parament) throws Exception {
        log.info("初始化数据库表：{}", parament.getTableConfig().getTableName());
        DataSourceUtil.init(new DataSourceConfig(parament.getDataSourceConfig().getDriver(), parament.getDataSourceConfig().getUrl(), parament.getDataSourceConfig().getUserName(), parament.getDataSourceConfig().getPassword()));
        TableInfo table = DataSourceUtil.getTableRemark(parament);
        table.setFieldList(DataSourceUtil.getField(parament.getTableConfig()));
        TableInfo tableInfo = StringUtil.removePrefix(table);
        TableInfo tableNh = StringUtil.namingHump(tableInfo);
        tableNh.setFieldList(FieldTypeUtil.typeMapping(tableNh.getFieldList()));
        return tableNh;
    }

    @Override
    public void generatorEntity(TableInfo tableInfo, CodeParament parament) throws Exception {
        tableInfo = this.createPath(tableInfo, parament, "entity");
        tableInfo.setGeneratorName(tableInfo.getBeanName());

        tableInfo.setEntityClassPackage(parament.getBaseInfoConfig().getPack() + ".entity");
        tableInfo.setVoClassPackage(tableInfo.getEntityClassPackage() + ".vo");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Entity.getType());
        if (parament.getBaseInfoConfig().getIsGeneratorSelect() || parament.getBaseInfoConfig().getIsSelectCustom()) {
            generatorVO(tableInfo, parament);
        }
    }

    @Override
    public void generatorMapper(TableInfo tableInfo, CodeParament parament) throws Exception {
        tableInfo = this.createPath(tableInfo, parament, "mapper");
        tableInfo.setGeneratorName(tableInfo.getBeanName() + "Mapper");
        tableInfo.setMapperClassPackage(parament.getBaseInfoConfig().getPack() + ".mapper");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Mapper.getType());
    }

    @Override
    public void generatorMapperXml(TableInfo tableInfo, CodeParament parament) throws Exception {
        if (tableInfo.getMapperXmlUrl() == null||"".equals(tableInfo.getMapperXmlUrl())) {
            StringBuilder pathBuild = new StringBuilder(parament.getBaseInfoConfig().getPath());
            String pack = parament.getBaseInfoConfig().getPack();
            String packs = pack.replaceAll("\\.", "/");
            pathBuild.append(packs).append("/mapper");
            FileUtil.create(pathBuild.toString());
            tableInfo.setMapperXmlUrl(pathBuild.toString());
        } else {
            FileUtil.create(parament.getBaseInfoConfig().getMapperXmlUrl());
        }
        tableInfo.setGeneratorUrl(tableInfo.getMapperXmlUrl());
        tableInfo.setGeneratorName(tableInfo.getBeanName() + "Mapper");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Mapper_XML.getType());
    }

    @Override
    public void generatorService(TableInfo tableInfo, CodeParament parament) throws Exception {
        tableInfo = this.createPath(tableInfo, parament, "service");
        tableInfo.setGeneratorName(tableInfo.getBeanName() + "Service");
        tableInfo.setServiceClassPackage(parament.getBaseInfoConfig().getPack() + ".service");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Service.getType());
        this.generatorServiceImpl(tableInfo);
    }

    @Override
    public void generatorController(TableInfo tableInfo, CodeParament parament) throws Exception {
        tableInfo = this.createPath(tableInfo, parament, "controller");
        tableInfo.setGeneratorName(tableInfo.getBeanName() + "Controller");
        tableInfo.setControllerClassPackage(parament.getBaseInfoConfig().getPack() + ".controller");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Controller.getType());
    }

    @Override
    public void generatorR(TableInfo tableInfo, CodeParament parament) throws Exception {
        tableInfo = this.createPath(tableInfo, parament, "util");
        tableInfo.setGeneratorName("R");
        tableInfo.setReClassPackage(parament.getBaseInfoConfig().getPack() + ".util");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Util_R.getType());
    }


    public void generatorVO(TableInfo tableInfo, CodeParament parament) throws Exception {
        String path = tableInfo.getGeneratorUrl() + "/vo";
        FileUtil.create(path);
        tableInfo.setGeneratorUrl(path);
        tableInfo.setGeneratorName("GeneratorVo");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Util_VO.getType());
    }

    @Override
    public void generatorMybatisPlusConfig(TableInfo tableInfo, CodeParament parament) throws Exception {
        tableInfo = this.createPath(tableInfo, parament, "config");
        tableInfo.setGeneratorName("MybatisPlusConfig");
        tableInfo.setMybatisPlusClassPackage(parament.getBaseInfoConfig().getPack() + ".config");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_MybatisPlus_Config.getType());

    }

    @Override
    public void generatorExcelImport(TableInfo tableInfo, CodeParament parament) throws Exception {
        tableInfo = this.createPath(tableInfo, parament, "listener");
        tableInfo.setGeneratorName(tableInfo.getBeanName() + "Listener");
        tableInfo.setExcelImportClassPackage(parament.getBaseInfoConfig().getPack() + ".listener");
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Excel_Import.getType());
    }


    private void generatorServiceImpl(TableInfo tableInfo) throws Exception {
        String path = tableInfo.getGeneratorUrl() + "/impl";
        FileUtil.create(path);
        tableInfo.setGeneratorName(tableInfo.getBeanName() + "ServiceImp");
        tableInfo.setServiceImplClassPackage(tableInfo.getServiceClassPackage() + ".impl");
        tableInfo.setGeneratorUrl(path);
        GeneratorUtil.generatorJavaCode(tableInfo, GeneratorTypeEnum.JAVA_Service_Impl.getType());
    }

    private TableInfo createPath(TableInfo tableInfo, CodeParament parament, String endPath) throws Exception {
        StringBuilder pathBuild = new StringBuilder(parament.getBaseInfoConfig().getPath());
        String pack = parament.getBaseInfoConfig().getPack();
        String packs = pack.replaceAll("\\.", "/");
        pathBuild.append(packs).append("/").append(endPath);
        FileUtil.create(pathBuild.toString());
        tableInfo.setGeneratorUrl(pathBuild.toString());
        return tableInfo;
    }

}
